package com.markus.code.图.图1;

import java.util.HashSet;
import java.util.Stack;

/**
 * Author:markusZhang
 * Date:Create in 2020/8/19 11:26
 * todo: 图的深度优先遍历
 */
public class GraphDfs {
    public void dfs(Node node){
        if (node == null){
            return ;
        }
        Stack<Node> stack = new Stack<>();
        HashSet<Node> set = new HashSet<>();
        stack.push(node);
        set.add(node);
        //入栈就打印
        System.out.print(node.value+" ");
        while(!stack.isEmpty()){
            Node pre = stack.pop();
            for (Node next : pre.nexts) {
                if (!set.contains(next)){
                    stack.push(pre);
                    stack.push(next);
                    set.add(next);
                    System.out.print(next.value+" ");
                    break;
                }
            }
        }
    }
}
